package org.ycb.ycbisgod.ycbwoxihuanni.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.ycb.ycbisgod.ycbwoxihuanni.entity.Achievements;
import org.ycb.ycbisgod.ycbwoxihuanni.service.IAchievementsService;
import org.ycb.ycbisgod.ycbwoxihuanni.util.Result;
import org.ycb.ycbisgod.ycbwoxihuanni.constant.MessageConstant;

import java.util.List;


@Slf4j
@RestController
@RequestMapping("/achievement")
public class AchievementsController {
    @Autowired
    private IAchievementsService AchievementsService;

    @RequestMapping("/list")
    public Result list() {
        List<Achievements> achievementsList = AchievementsService.list();
        return new Result(true, MessageConstant.ACHIEVEMENT_QUERY_SUCCESS, achievementsList);
    }
    @RequestMapping("/add")
    public Result add(@RequestBody Achievements achievements) {
        try {
            AchievementsService.save(achievements);
            return new Result(true, MessageConstant.ACHIEVEMENT_ADD_SUCCESS);
        } catch (Exception e) {
            log.error("添加失败: {}", e.getMessage());
            return new Result(false, MessageConstant.ACHIEVEMENT_ADD_FAIL);
        }
    }
}
